package com.jacen.fzxs.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.jacen.fzxs.entity.UserEntity;
import com.jacen.fzxs.util.Constants;
import com.jacen.fzxs.web.Pager;

/**
 * 基础Controller
 * @author Jacen
 * 2014年9月22日 上午9:35:14
 */
public class BaseController {
	
	/**
	 * 登录用户
	 */
	private static UserEntity localUser = null;
	
	private Pager pager;
	
	/**
	 * 获取用户
	 * @param request
	 * @return UserEntity
	 */
	private static UserEntity getUser(HttpServletRequest request){
		HttpSession session = request.getSession();
		return (UserEntity) session.getAttribute(LoginController.USERNAME);
	}
	
	/**
	 * 获取分页
	 * @param request
	 */
	public Pager getPager(Integer curPage,HttpServletRequest request){
		if(pager==null){
			pager = new Pager();
		}
		if(curPage==null || curPage.intValue()<=0)curPage = 1;
		pager.setPageSize(Constants.PAGE_SIZE);
		pager.setCurPageNum(curPage.intValue());
		request.setAttribute(Constants.PAGER, pager);
		return pager;
	}
	
	/**
	 * 获取当前登录用户
	 * @param request
	 * @return UserEntity
	 */
	public static UserEntity getLocalUser(HttpServletRequest request){
		if(localUser==null){
			localUser = getUser(request);
		}
		return localUser;
	}
	
	/**
	 * 清除当前登录用户
	 */
	public static void clearLocalUser(){
		localUser = null;
	}

}
